package com.mango.module.ldap.mapper;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.ldap.core.AttributesMapper;

import com.mango.module.ldap.domain.PersonDO;


/**
 * 用户字段映射
 * @author Mango-Lab
 * @Date 2021年4月8日
 */
public class PersonAttributesMapper implements AttributesMapper<PersonDO> {
    /**
     * Map Attributes to an object. The supplied attributes are the attributes
     * from a single SearchResult.
     *
     * @param attrs attributes from a SearchResult.
     * @return an object built from the attributes.
     * @throws NamingException if any error occurs mapping the attributes
     */
    @Override
    public PersonDO mapFromAttributes(Attributes attrs) throws NamingException {
        PersonDO person = new PersonDO();
        person.setUserId(attrs.get("uid")==null?"":attrs.get("uid").get().toString());
        person.setName(attrs.get("cn")==null?"":attrs.get("cn").get().toString());
        person.setRoleId(attrs.get("roleId")==null?"":attrs.get("roleId").get().toString());
        person.setRoleName(attrs.get("roleName")==null?"":attrs.get("roleName").get().toString());
        person.setUserPassword(attrs.get("userPassword")==null?"":new String((byte [])attrs.get("userPassword").get()));
        person.setGivenName(attrs.get("givenname")==null?"":attrs.get("givenname").get().toString());
        return person;
    }
}

